using System;
using System.Runtime.InteropServices;
using Framework;
using UnityEngine;

namespace Proj
{
    public static class OpenSelectFileWindow
    {
        public static string OpenFile(string title, string filterExt, string initialDir = "")
        {
#if UNITY_EDITOR
            OpenFileName openFileName = new OpenFileName();
            openFileName.structSize = Marshal.SizeOf(openFileName);
            openFileName.filter = filterExt;//"Excel文件(*.xlsx)\0*.xlsx";
            openFileName.file = new string(new char[256]);
            openFileName.maxFile = openFileName.file.Length;
            openFileName.fileTitle = new string(new char[64]);
            openFileName.maxFileTitle = openFileName.fileTitle.Length;
            openFileName.initialDir = initialDir.Replace('/', '\\');
            openFileName.title = title;
            openFileName.flags = 0x00080000 | 0x00001000 | 0x00000800 | 0x00000008;
            if (GetOpenFileName(openFileName))
            {
                return openFileName.file;
            }
#endif
            return "";
        }

        public static void OpenExplorer(string dir)
        {
#if UNITY_EDITOR
            System.Diagnostics.Process.Start("explorer.exe", dir.Replace("/", "\\"));
#endif
        }

        public static string CalcMd5(string path)
        {
            return MD5Util.ComputeFileMd5(path);
        }

        //链接指定系统函数       打开文件对话框
#if UNITY_EDITOR
        [DllImport("Comdlg32.dll", SetLastError = true, ThrowOnUnmappableChar = true, CharSet = CharSet.Auto)]

        private static extern bool GetOpenFileName([In, Out] OpenFileName ofn);

        private static bool GetOFN([In, Out] OpenFileName ofn)
        {
            return GetOpenFileName(ofn);
        }
#endif

        //链接指定系统函数        另存为对话框
#if UNITY_EDITOR
        [DllImport("Comdlg32.dll", SetLastError = true, ThrowOnUnmappableChar = true, CharSet = CharSet.Auto)]

        private static extern bool GetSaveFileName([In, Out] OpenFileName ofn);

        private static bool GetSFN([In, Out] OpenFileName ofn)
        {
            return GetSaveFileName(ofn);
        }
#endif
    }

    [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
    public class OpenFileName
    {
        public int structSize = 0;
        public IntPtr dlgOwner = IntPtr.Zero;
        public IntPtr instance = IntPtr.Zero;
        public String filter = null;
        public String customFilter = null;
        public int maxCustFilter = 0;
        public int filterIndex = 0;
        public String file = null;
        public int maxFile = 0;
        public String fileTitle = null;
        public int maxFileTitle = 0;
        public String initialDir = null;
        public String title = null;
        public int flags = 0;
        public short fileOffset = 0;
        public short fileExtension = 0;
        public String defExt = null;
        public IntPtr custData = IntPtr.Zero;
        public IntPtr hook = IntPtr.Zero;
        public String templateName = null;
        public IntPtr reservedPtr = IntPtr.Zero;
        public int reservedInt = 0;
        public int flagsEx = 0;
    }
}